﻿
using BPMN.Domid.IReposict;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.infarstrue.Reposict
{
    public class ReposictBase<T>: IReposictBase<T> where T : class
    {
        private readonly BPMNDbContext db;
        private IDbContextTransaction tran;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public ReposictBase(BPMNDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await SaveChange();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> AddRolu(List<T> entity)
        {
            await db.Set<T>().AddRangeAsync(entity);
            return await SaveChange();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> Delete(int id)
        {
            var entity = await db.Set<T>().FindAsync(id);
            if (entity != null)
            {
                db.Set<T>().Remove(entity);
            }
            return await SaveChange();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<int> ResDel(List<T> list)
        {
            await Task.Run(async () => db.Set<T>().RemoveRange(list));
            return await SaveChange();
        }
        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetAll()
        {
            return await Task.Run(async () => db.Set<T>().AsQueryable());
        }
        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> Get(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetListByPage(int pageIndex = 1, int pageSize = 2)
        {
            var query = db.Set<T>();
            var count = await query.CountAsync();
            return new { count = count, list = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArrayAsync() };
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Update(T entity)
        {
           await Task.Run( async()=>db.Set<T>().Update(entity));
            return await SaveChange();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entiy"></param>
        /// <returns></returns>
        public async Task<int> RoluUpd(List<T> entiy)
        {
            await Task.Run(async () => db.Set<T>().UpdateRange(entiy));
            return await SaveChange();
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <returns></returns>
        public async Task<int> SaveChange()
        {
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 开启事务
        /// </summary>
        public void BeginTran()
        {
            tran = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable);
        }
        /// <summary>
        /// 提交事务
        /// </summary>
        public void CommitTran()
        {
            tran.Commit();
        }
        /// <summary>
        /// 事务回滚
        /// </summary>
        public void RollbakTran()
        {
            tran.Rollback();
        }
       
        
    }
}
